package com.example.idachuappone.utils;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.http.Header;
import org.apache.http.message.BasicHeader;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Build.VERSION;
import android.os.Handler;
import android.os.Message;
import android.support.v4.media.TransportMediator;

import com.example.idachuappone.R;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class NetUtil {

	static class AnonymousClass_1 extends Handler {
		private final/* synthetic */ImageCallBack val$callBack;

		AnonymousClass_1(ImageCallBack imageCallBack) {
			this.val$callBack = imageCallBack;
		}

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			this.val$callBack.imageLoaded((Drawable) msg.obj, String.valueOf(msg.arg1));
		}
	}

	static class AnonymousClass_2 extends Thread {
		private final/* synthetic */String val$image;
		private final/* synthetic */Handler val$servHandler;
		private final/* synthetic */String val$strUrl;

		AnonymousClass_2(String str, String str2, Handler handler) {
			this.val$strUrl = str;
			this.val$image = str2;
			this.val$servHandler = handler;
		}

		@Override
		public void run() {
			BitmapDrawable drawable = (BitmapDrawable) Drawable.createFromStream(
					NetUtil.getHttpStream(this.val$strUrl, this.val$image), this.val$image);
			if (drawable != null) {
				Message msg = this.val$servHandler.obtainMessage();
				msg.arg1 = Integer.parseInt(this.val$image);
				msg.obj = drawable;
				this.val$servHandler.sendMessage(msg);
			}
		}
	}

	public static interface ImageCallBack {
		void imageLoaded(Drawable drawable, String str);
	}

	private static synchronized InputStream httpGet(String url) throws Exception {
		InputStream inputStream;
		synchronized (NetUtil.class) {
			inputStream = ((HttpURLConnection) new URL(url).openConnection()).getInputStream();
		}
		return inputStream;
	}

	private static InputStream getHttpStream(String strUrl, String image) {
		try {
			return httpGet(new StringBuilder(String.valueOf(strUrl)).append("&image=").append(image).toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Drawable getNetImage(String strUrl, String image, ImageCallBack callBack) {
		new AnonymousClass_2(strUrl, image, new AnonymousClass_1(callBack)).start();
		return null;
	}

	public static String unescapeUnicode(String str) {
		StringBuffer sb = new StringBuffer();
		Matcher matcher = Pattern.compile("\\\\u([0-9a-fA-F]{4})").matcher(str);
		while (matcher.find()) {
			matcher.appendReplacement(
					sb,
					new StringBuilder(String.valueOf((char) Integer.parseInt(matcher.group(1),
							TransportMediator.FLAG_KEY_MEDIA_PAUSE))).toString());
		}
		matcher.appendTail(sb);
		return sb.toString().replace("\\", "");
	}

	public static String unescapeUnicodealipay(String str) {
		StringBuffer sb = new StringBuffer();
		Matcher matcher = Pattern.compile("\\\\u([0-9a-fA-F]{4})").matcher(str);
		while (matcher.find()) {
			matcher.appendReplacement(
					sb,
					new StringBuilder(String.valueOf((char) Integer.parseInt(matcher.group(1),
							TransportMediator.FLAG_KEY_MEDIA_PAUSE))).toString());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}

	public static String unEscapeUnicode(String str) {
		StringBuffer sb = new StringBuffer();
		Matcher matcher = Pattern.compile("\\\\u([0-9a-fA-F]{4})").matcher(str);
		while (matcher.find()) {
			matcher.appendReplacement(
					sb,
					new StringBuilder(String.valueOf((char) Integer.parseInt(matcher.group(1),
							TransportMediator.FLAG_KEY_MEDIA_PAUSE))).toString());
		}
		matcher.appendTail(sb);
		return sb.toString().replace("rn", "").replace("\\", "");
	}

	public static void get(Context context, String url, int cachems, RequestCallBack<String> callback) {
		List<Header> headers = getHeaders(context);
		RequestParams params = new RequestParams();
		params.setHeaders(headers);
		HttpUtils httpUtils = new HttpUtils();
		httpUtils.configCurrentHttpCacheExpiry(cachems);
		httpUtils.send(HttpMethod.GET, url, params, callback);
	}

	public static void post(Context context, String url, Map<String, String> map, RequestCallBack<String> callback) {
		RequestParams params = new RequestParams();
		HttpUtils httpUtils = new HttpUtils();
		params.setHeaders(getHeaders(context));
		for (String temp : map.keySet()) {
			params.addBodyParameter(temp, map.get(temp));
		}
		httpUtils.send(HttpMethod.POST, url, params, callback);
	}

	public static void netFail(Context context, Dialog dialog, boolean isDialog) {
		if (isDialog) {
			dialog.dismiss();
		}
		MainToast.show(context, context.getResources().getString(R.string.netFail), 0);
	}

	public static List<Header> getHeaders(Context context) {
		String a = URLEncoder.encode(String.valueOf(PrefUtil.getInstance(context).getAuth()));
		String model = Build.MODEL;
		String release = VERSION.RELEASE;
		String fingerprint = Build.FINGERPRINT;
		String str = "";
		try {
			str = fingerprint.substring(0, fingerprint.lastIndexOf(":user"));
			str = str.substring(str.lastIndexOf("/") + 1);
		} catch (Exception e) {
			str = "no miuid";
		}
		List<Header> headers = new ArrayList();
		Header header1 = new BasicHeader("Cookie", "auth=" + a + "; city_id=" + AppShareData.city_id);
		Header header2 = new BasicHeader("User-Agent", new StringBuilder(String.valueOf(model)).append("; Android ")
				.append(release).append(" ").append(str).append("; idachu ").append(getVersiongetVersion(context))
				.toString());
		headers.add(header1);
		headers.add(header2);
		return headers;
	}

	public static String getVersiongetVersion(Context context) {
		try {
			return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
		} catch (Exception e) {
			e.printStackTrace();
			return "-1";
		}
	}
}
